/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：编辑器扩展的工具类
* CreateTime：2020-04-05 19:06:34
* Version：1.0
* Modify Recoder：
*************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace TinyToolKitEditor
{
    public static class TinyEditorUtility
    {
        /// <summary>
        /// 根据路径，寻找该路径下所有的预制体，并返回所有挂载 T 的对象
        /// </summary>
        /// <typeparam name="T">需要查询的类型</typeparam>
        /// <param name="path">需要查询的路径</param>
        /// <returns>返回该路径下所有挂载该类型的对象数组</returns>
        public static List<T> GetAllAssets<T>(string path) where T : MonoBehaviour
        {
            string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] { path });
            List<T> assetList = new List<T>();

            for (int i = 0; i < guids.Length; i++)
            {
                string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
                GameObject asset = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
                T temp = asset.GetComponent<T>();
                if (temp != null)
                    assetList.Add(temp);
            }
            return assetList;
        }
        
        public static void Copy(string format,params object[] args)
        {
            string result = string.Format(format,args);
            TextEditor editor = new TextEditor {content = new GUIContent(result)};
            editor.OnFocus();
            editor.Copy();
        }
    }
}

